package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {

    /**
     * 插入订单数据
     * @param orders
     */
    void insert(Orders orders);
    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 分页查看历史订单
     * @param ordersPageQueryDTO
     * @return
     */
    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据订单id查询订单
     * @param id
     * @return
     */
    @Select("select * from orders where id=#{id}")
    Orders getById(Long id);

    /**
     * 各个状态的订单数量统计
     * @param status
     * @return
     */
    @Select("select count(orders.id) from orders where status=#{status}")
    Integer countStatus(Integer status);

    /**
     * 用于替换微信支付更新数据库状态的问题
     * @param orderStatus
     * @param orderPaidStatus
     */
    @Update("update orders set status = #{orderStatus},pay_status = #{orderPaidStatus} ,checkout_time = #{checkOutTime} " +
            "where number = #{orderNumber}")
    void updateStatus(Integer orderStatus, Integer orderPaidStatus, LocalDateTime checkOutTime, String orderNumber);

    /**
     * 查询未付款订单是否超时
     * @param status
     * @param orderTime
     * @return
     */
    @Select("select * from orders where status=#{status} and order_time<#{orderTime}")
    List<Orders> getBystatusAndOrderTimeLT(Integer status,LocalDateTime orderTime);

    /**
     * 统计营业额
     * @param map
     * @return
     */
    @Select("select sum(amount) from orders where status=#{status} and order_time>=#{begin} and order_time <= #{end}")
    Double sumByMap(Map map);

    /**
     * 统计用户总量
     * @param map
     */
    @Select("select count(id) from user where  create_time<=#{end}")
    Integer getByCreatTime(Map map);

    /**
     * 获取用户id
     * @param map
     * @return
     */
    @Select("select count(id) from user where create_time>=#{begin} and create_time<=#{end}")
    Long getUserId(Map map);

    /**
     * 每天的订单数
     * @param map
     * @return
     */
    @Select("select count(id) from orders where orders.order_time>=#{begin} and orders.order_time<=#{end} ")
    Integer getDailyOrderCount(Map map);

    /**
     * 订单总数
     * @param map
     * @return
     */
    @Select("select count(id) from orders where order_time<=#{end}")
    Integer getTotalOrderCount(Map map);

    /**
     * 每天有效订单数
     * @param map
     * @return
     */
    @Select("select count(orders.id) from orders where orders.order_time>=#{begin} and orders.order_time<=#{end} and status=#{status}")
    Integer getDailyValidOrderCount(Map map);

    /**
     * 有效订单总数
     * @param map
     * @return
     */
    @Select("select count(orders.id) from orders where orders.order_time<=#{end} and status = #{status}")
    Integer getValidOrderCount(Map map);

    /**
     * 查询订单id
     * @param map
     * @return
     */
    @Select("select id from orders where orders.order_time>=#{begin} and orders.order_time<=#{end} and status=#{status}")
    List<Long>  getByOrderId(Map map);

    /**
     * 统计指定时间区间欸的销量排名前10
     * @param begin
     * @param end
     * @return
     */
    List<GoodsSalesDTO> getSalesTop10(LocalDateTime begin,LocalDateTime end);

    /**
     * 根据map集合条件查询
     * @param map
     * @return
     */
    Integer countByMap(Map map);
}
